h2. Prototyping with Quick Forms
<br>

<h4>Rails and Formtastic Code Example</h4>

Four lines of Formtastic code

<pre>
<% semantic_form_for @book do |form| %>
    <%= form.inputs %>
    <%= form.buttons %>
<% end %>
</pre>

are placed in a partial file: 

<pre>
 _form.html.erb 
</pre>

in a Rails root folder "shared_partials" which in combination with the Rails MVC files and applied migrations files renders this "HTML form":http://wiki.github.com/justinfrench/formtastic/71-example-rendered-html

<h4>Controller Action New</h4>

<pre>
  def new
    @book = Book.new
    respond_to do |format|
      format.html # new.html.erb
    end
  end
</pre>

<h4>New View</h4>

<pre>
<h1>Add a Book</h1>
<%= render(:partial => '../shared_partials/form') %>
</pre>

<h4>Edit View</h4>

<pre>
<h1>Edit a Book</h1>
<%= render(:partial => '../shared_partials/form') %>
</pre>

<h4>Models</h4>

These models setup the table associates: belongs_to, has_many and has_and_belongs_to_many (habtm) 

<pre>
class Book < ActiveRecord::Base
  belongs_to :publisher
  has_and_belongs_to_many :authors
  has_and_belongs_to_many :format_types
end
</pre>

<pre>
class Author < ActiveRecord::Base
 has_and_belongs_to_many :books
end
</pre>

<pre>
class FormatType < ActiveRecord::Base
  has_and_belongs_to_many :books
end
</pre>

<pre>
class Publisher < ActiveRecord::Base
  has_many :books
end
</pre>

<a name="migration_file_books"></a><h4>Table Migration Files</h4>

<pre>
class CreateBooks < ActiveRecord::Migration
  def self.up
    create_table :books do |t|
      t.string :title
      t.text :description
      t.boolean :still_in_print
      t.date :published
      t.integer :isbn_number
      t.decimal :price, :precision => 8, :scale => 2
      t.integer :publisher_id
      t.timestamps
    end
  end

  def self.down
    drop_table :books
  end
end
</pre>

<pre>
class CreatePublishers < ActiveRecord::Migration
  def self.up
    create_table :publishers do |t|
      t.string :name
      t.timestamps
    end
  end

  def self.down
    drop_table :publishers
  end
end
</pre>

<pre>
class CreateAuthors < ActiveRecord::Migration
  def self.up
    create_table :authors do |t|
      t.string :name
      t.timestamps
    end
  end

  def self.down
    drop_table :authors
  end
end
</pre>

<pre>
class CreateJoinAuthorsBooks < ActiveRecord::Migration
  def self.up
    create_table :authors_books, :id => false do |t|
      t.integer :author_id
      t.integer :book_id
    end
    add_index :authors_books, [:author_id, :book_id], :unique => true
    add_index :authors_books, :book_id, :unique => false
  end

  def self.down
    drop_table :authors_books
  end
end
</pre>

<pre>
class CreateFormatTypes < ActiveRecord::Migration
  def self.up
    create_table :format_types do |t|
      t.string :name
      t.timestamps
    end
  end

  def self.down
    drop_table :format_types
  end
end
</pre>

<pre>
class CreateJoinBooksFormatTypes < ActiveRecord::Migration
  def self.up
    create_table :books_format_types, :id => false do |t|
      t.integer :book_id
      t.integer :format_type_id
    end
    add_index :books_format_types, [:book_id, :format_type_id], :unique => true
    add_index :books_format_types, :format_type_id, :unique => false
  end

  def self.down
    drop_table :books_format_types
  end
end
</pre>